#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <asl.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWDetailListEntry.h"
#import "framework/FWDetailListSection.h"
#import "framework/FWPlugin.h"

#import "PICommon.h"
#import "PIPlugin.h"
#import "PIDebugView.h"


static NSString* const kPluginPrefsPage = @"Plugin";
static NSString* const kPIPrefsPage = @"PI";


@implementation PIDebugView


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management


// ******************************************************************************************
#pragma mark -
#pragma mark Handling user input

- (BOOL)doneClicked:(FWApplication*)application sections:(NSArray*)sections
{
    return YES;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Standard debug info

-(NSArray*)console
{
    NSMutableArray *consoleLog = [NSMutableArray array];
    
    aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);
    
    aslmsg query = asl_new(ASL_TYPE_QUERY);
    asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
    aslresponse response = asl_search(client, query);
    
    asl_free(query);
    
    aslmsg message;
    while((message = aslresponse_next(response)))
    {
        const char *msg = asl_get(message, ASL_KEY_MSG);
        if (msg)
            [consoleLog addObject:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]];
    }
    
    aslresponse_free(response);
    asl_close(client);
    
    return consoleLog;
}

- (NSArray*)dataForDetailListPage:(NSObject *)page key:(NSObject*)key application:(FWApplication *)application
{  
    NSArray* infoSect = [NSArray arrayWithObjects:
                         [FWDetailListEntry entryWithTitle:@"Pocket Informant Version"
                                                     value:((PIPlugin*)application.fwPlugin).isPocketInformant2 ? @"2" : @"1"],
                         nil];

    NSArray* prefsSect = [NSArray arrayWithObjects:
                          [[FWDetailListEntry entryWithTitle:@"Plugin Preferences"]
                           withLinkType:kPluginPrefsPage],
                          [[FWDetailListEntry entryWithTitle:@"Informant Preferences"]
                           withLinkType:kPIPrefsPage],
                          nil];

    NSMutableArray* display = [NSMutableArray arrayWithObjects:
                               [FWDetailListSection sectionForElements:infoSect],
                               [FWDetailListSection sectionForElements:prefsSect withTitle:@"Preferences"],
                               nil];
    
    NSArray* extras = [self detailSectionsForListPage:page key:key application:application];
    if (extras)
        [display addObjectsFromArray:extras];
    
    NSTimeZone* tz = [NSTimeZone systemTimeZone];
    NSDate* now = [NSDate date];
    NSDate *localMidnight = FWLocalDateForLocalMidnight();
    NSDate *utcMidnight = FWUTCForLocalDate(localMidnight);
    
    NSArray* info = [NSArray arrayWithObjects:
                     [FWDetailListEntry entryWithTitle:@"Timezone name"
                                                 value:tz.name],
                     [FWDetailListEntry entryWithTitle:@"Timezone code"
                                                 value:tz.abbreviation],
                     [FWDetailListEntry entryWithTitle:@"Offset"
                                                 value:[NSNumber numberWithInt:tz.secondsFromGMT]],
                     [FWDetailListEntry entryWithTitle:@"Now"
                                                 value:now.description],
                     [FWDetailListEntry entryWithTitle:@"Start"
                                                 value:utcMidnight.description],
                     [FWDetailListEntry entryWithTitle:@"UTC"
                                                 value:localMidnight.description],
                     [FWDetailListEntry entryWithTitle:@"DST"
                                                 value:[NSNumber numberWithBool:tz.isDaylightSavingTime]],
                     nil];
    
    [display addObject:[FWDetailListSection sectionForElements:info withTitle:@"Local Time"]];
    
    return display;
}

- (NSString*)captionForListPage:(NSObject *)page forKey:(NSObject *)key displayDepth:(NSInteger)depth application:(FWApplication *)application
{
    if ([(NSString*)key isEqualToString:kPluginPrefsPage])
        return @"Plugin Prefs";
    else if ([(NSString*)key isEqualToString:kPIPrefsPage])
        return @"Informant Prefs";
    
    return nil;
}

- (NSString*)listPageTextForKey:(NSObject*)key currentPage:(NSObject*)current displayDepth:(NSInteger)depth application:(FWApplication*)application
{
    if ([(NSString*)key isEqualToString:kPluginPrefsPage]) {
        return [application.fwPlugin.liPlugin.preferences description];
    }
    else if ([(NSString*)key isEqualToString:kPIPrefsPage]) {
        NSDictionary* applicationPrefs = [NSDictionary dictionaryWithContentsOfFile:application.preferencesPath];
        return [applicationPrefs description];
    }
    
    return nil;
}

- (NSArray*)detailSectionsForListPage:(NSObject *)page key:(NSObject *)key application:(FWApplication *)application
{
    return nil;
}

@end
